extends Node2D

class AchievementDesc:
	var name : String
	var conditionChecker : Callable
	var callback : Callable
	var node : Node

	func _init(_name: String, _node: Node) -> void:
		name = _name
		conditionChecker = _node.conditionChecker
		callback = _node.callback
		node = _node

var AchievementMap = {
}

func _register_achievement(_name: String):
	AchievementMap[_name] = AchievementDesc.new(_name, get_node(_name))

func init() -> void:
	_register_achievement("Ending1")
	_register_achievement("Ending2")
	_register_achievement("enter_night_once")
	_register_achievement("Rainbow")
	_register_achievement("Light_Master")

func _process(_delta: float) -> void:
	for achievement_name in AchievementMap:
		var desc = AchievementMap[achievement_name]
		if !desc.node.is_achieved and desc.conditionChecker.call():
			_achieve(desc)

func _achieve(desc: AchievementDesc) -> void:
	if desc.node.is_achieved: return

	desc.node.is_achieved = true
	desc.callback.call()

func _reset(desc: AchievementDesc) -> void:
	desc.node.is_achieved = false

func achieve(_name: String) -> void:
	var desc = AchievementMap.get(_name)
	if desc != null: _achieve(desc)

func reset(_name: String) -> void:
	var desc = AchievementMap.get(_name)
	if desc != null: _reset(desc)

func is_achieved(_name: String) -> bool:
	var desc = AchievementMap.get(_name)
	if desc == null:
		return false
	return desc.node.is_achieved
